package com.winning.vsts.merge.service.jpa;

import com.winning.vsts.merge.service.domain.PullRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * pull request 请求
 *
 * @author xcl <xcl@winning.com.cn>
 * @date 2021/11/18
 */
@Repository
public interface PullRequestJpaRepository extends JpaRepository<PullRequest, Integer> {


    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query("update pull_request set vstsId = ?2,status = ?3, mergeStatus = ?4,updateTime = ?5,requestUrl=?6 where id " +
        "= ?1")
    int updatePullRequestStatus(Integer pullRequestId, String vstsId, String status, String mergeStatus,
                                Date updateDate, String requestUrl);


    @Query("select pr from pull_request pr where pr.status = 'active' and pr.mergeStatus not in ('succeeded'," +
        "'conflicts','failure')")
    List<PullRequest> queryNotCompleted();


    @Query("select pr from pull_request pr where pr.status = 'active' ")
    List<PullRequest> queryActive();
}
